package ca.polymtl.rubikcube.CubeActivity.model;

import ca.polymtl.rubikcube.CubeActivity.model.Side;
import ca.polymtl.rubikcube.CubeActivity.model.Rubik.RotationDirection;
import ca.polymtl.rubikcube.util.Position;

public class CubeTransitions {
	public Position[] corners = new Position[4];
	public Position[] sides = new Position[4];
	Position center;
	
	
	/**
	 * Returns the set of transition for the {@link Side} side. CCW is forward
	 * and CW is backward.
	 * @param side
	 * @return
	 */
	static CubeTransitions getTransitions(Side side, RotationDirection way) {
		int cte;
		int a;
		int b;
		int c;
		int d;
;
		/*
		 * If side is Front, Top or Right, the constant coord will be 2,
		 * else 0.
		 */
		if (side.ordinal() <= 5) {
			// Front, Top, Right
			if (side.ordinal() <= 2) {
				cte = 2;
			}
			else {
				cte = 0;
			}
			
		} else {
			// Left, Back, Bottom
			cte = 1;
		}
		
		if (side.ordinal() <= 2 && way == RotationDirection.CCW ||
				side.ordinal() >= 3 && way == RotationDirection.CW) {
			a = 0;
			b = 1;
			c = 2;
			d = 3;
		} else {
			// The two tables need to be � l'envers.
			a = 3;
			b = 2;
			c = 1;
			d = 0;
		}

		/*
		 * Create a new set of transitions.
		 */
		CubeTransitions t = new CubeTransitions();
		
		switch (side) {
		case FRONT:
		case BACK:
		case MIDDLE_FB:
			t.corners[a] = new Position(0, 0, cte);
			t.corners[b] = new Position(2, 0, cte);
			t.corners[c] = new Position(2, 2, cte);
			t.corners[d] = new Position(0, 2, cte);
			
			t.sides[a] = new Position(1, 0, cte);
			t.sides[b] = new Position(2, 1, cte);
			t.sides[c] = new Position(1, 2, cte);
			t.sides[d] = new Position(0, 1, cte);
			
			t.center = new Position(1, 1, cte);
			break;
		
		case UP:
		case DOWN:
		case MIDDLE_UD:
			t.corners[a] = new Position(0, cte, 0);
			t.corners[b] = new Position(0, cte, 2);
			t.corners[c] = new Position(2, cte, 2);
			t.corners[d] = new Position(2, cte, 0);
			
			t.sides[a] = new Position(0, cte, 1);
			t.sides[b] = new Position(1, cte, 2);
			t.sides[c] = new Position(2, cte, 1);
			t.sides[d] = new Position(1, cte, 0);
			
			t.center = new Position(1, cte, 1);
			break;
		
		
		case RIGHT:
		case LEFT:
		case MIDDLE_RL:
			t.corners[a] = new Position(cte, 2, 2);
			t.corners[b] = new Position(cte, 0, 2);
			t.corners[c] = new Position(cte, 0, 0);
			t.corners[d] = new Position(cte, 2, 0);
			
			t.sides[a] = new Position(cte, 1, 2);
			t.sides[b] = new Position(cte, 0, 1);
			t.sides[c] = new Position(cte, 1, 0);
			t.sides[d] = new Position(cte, 2, 1);
			
			t.center = new Position(cte, 1, 1);
			break;
		
		}
		
		return t;
	}
}
